package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import com.example.springboot.mapper.CartMapper;
import com.example.springboot.mapper.GoodsMapper;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.pojo.Cart;
import com.example.springboot.pojo.Orders;
import com.example.springboot.service.OrdersService;
import com.example.springboot.mapper.OrdersMapper;
import com.example.springboot.utils.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CartMapper cartMapper;
    @Override
    public void create(Orders orders) {
        orders.setOrdersNumber(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        for (Cart cart : orders.getCartData()){
            Orders dbOrders = new Orders();
            BeanUtils.copyProperties(orders, dbOrders);
            Map<String,Object> map = ThreadLocalUtil.get();
            Integer userid = (Integer) map.get("id");
            dbOrders.setGoodsNumber(cart.getGoodsnumber());
            dbOrders.setUserId(userid);
            dbOrders.setGoodsId(cart.getGoodsId());
            dbOrders.setPrice(cart.getGoodsPrice()*cart.getGoodsnumber());
            cartMapper.delete(cart.getId());
            goodsMapper.updateCount(cart.getGoodsId(),cart.getGoodsnumber());
            ordersMapper.create(dbOrders);
         }
    }

    @Override
    public void delete(Integer id) {
        ordersMapper.delete(id);
    }

    @Override
    public List<Orders> findAll() {
        return ordersMapper.findAll();
    }

    @Override
    public Orders findById(Integer id) {
        return ordersMapper.findById(id);
    }

    @Override
    public PageInfo<Orders> findByUserId(Integer pageNum, Integer pageSize) {
      PageHelper.startPage(pageNum, pageSize);
      Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");
        List<Orders> list = ordersMapper.findByUserId(userid);
        return PageInfo.of(list);
    }


    @Override
    public void update(Orders orders) {
        ordersMapper.update(orders);
    }
    @Override
    public PageInfo<Orders> selectPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Orders> list = ordersMapper.findAll();
        return PageInfo.of(list);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            ordersMapper.delete(id);
        }

    }
    @Override
    public Orders findByOrdersNumber(String ordersNumber) {
        return ordersMapper.findByOrdersNumber(ordersNumber);

    }
}
